package servlet;


import pojo.SupplierContact;
import service.SContactService;
import service.impl.SContactServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @auth 吴阳基
 * @date 2024/1/2 16:07
 */
@WebServlet("/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");

        String cname = req.getParameter("cname");
        int age = Integer.parseInt(req.getParameter("age"));
        String mobile = req.getParameter("mobile");
        int pid = Integer.parseInt(req.getParameter("pid"));
        String createdate = req.getParameter("createdate");

        SupplierContact supplierContact = new SupplierContact();
        supplierContact.setCname(cname);
        supplierContact.setAge(age);
        supplierContact.setMobile(mobile);
        supplierContact.setPid(pid);
        supplierContact.setCreatedate(createdate);

        SContactService scs = new SContactServiceImpl();
        int result = scs.add(supplierContact);

        PrintWriter out = resp.getWriter();
        if (result > 0) {
            out.print("<script>alert('添加成功');location.href='list'</script>");
        } else {
            out.print("<script>alert('添加失败');history.back()</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
